/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.image;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.ConfigurationPort;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.OutputPort;

/**
 * ImageLoaderNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class ImageLoaderNode extends AbstractNode {

	/**
	 * CONFIG_IMAGEURI
	 */
	public static final String CONFIG_IMAGEURI = "imageURI";

	/**
	 * OUT_COLORMODEL
	 */
	public static final String OUT_COLORMODEL = "COLORMODEL";

	/**
	 * OUT_BUFFEREDIMAGE
	 */
	public static final String OUT_BUFFEREDIMAGE = "IMAGE";

	/**
	 * OUT_WIDTH
	 */
	public static final String OUT_WIDTH = "WIDTH";

	/**
	 * OUT_HEIGHT
	 */
	public static final String OUT_HEIGHT = "HEIGHT";

	/**
	 * OUT_RASTER
	 */
	public static final String OUT_RASTER = "RASTER";

	/**
	 * OUT_TYPE
	 */
	public static final String OUT_TYPE = "TYPE";

	/**
	 * colorModelPort
	 */
	private OutputPort colorModelPort;

	/**
	 * rasterPort
	 */
	private OutputPort bufferedImagePort;

	/**
	 * widthPort
	 */
	private OutputPort widthPort;

	/**
	 * heightPort
	 */
	private OutputPort heightPort;

	/**
	 * rasterPort
	 */
	private OutputPort rasterPort;

	/**
	 * typePort
	 */
	private OutputPort typePort;

	/**
	 * imageURI
	 */
	private InputPort imageURI;

	/**
	 * ImageLoaderNode Constructor
	 * 
	 */
	public ImageLoaderNode() {
		this("IMAGELOADER" + nr++, false);
	}

	/**
	 * ImageLoaderNode Constructor
	 * 
	 * @param name
	 * @param imageURI
	 * @param continuous
	 */
	public ImageLoaderNode(String name, boolean continuous) {
		super(name, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	@Override
	protected void createPorts() {
		colorModelPort = addOutputPort(new OutputPort(OUT_COLORMODEL, this, ColorModel.class));
		bufferedImagePort = addOutputPort(new OutputPort(OUT_BUFFEREDIMAGE, this, BufferedImage.class));
		widthPort = addOutputPort(new OutputPort(OUT_WIDTH, this, Integer.class));
		heightPort = addOutputPort(new OutputPort(OUT_HEIGHT, this, Integer.class));
		rasterPort = addOutputPort(new OutputPort(OUT_RASTER, this, Raster.class));
		typePort = addOutputPort(new OutputPort(OUT_TYPE, this, Integer.class));
		imageURI = addConfigurationPort(new ConfigurationPort(CONFIG_IMAGEURI, this, String.class));
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	@Override
	public void compute() {
		String uriString = (String) imageURI.fetchData();
		File f = new File(uriString);
		try {
			BufferedImage bi = ImageIO.read(f);
			bufferedImagePort.pushData(bi);
			widthPort.pushData(new Integer(bi.getWidth()));
			heightPort.pushData(new Integer(bi.getHeight()));
			rasterPort.pushData(bi.getRaster());
			typePort.pushData(new Integer(bi.getType()));
			colorModelPort.pushData(bi.getColorModel());
		} catch (IOException e) {
			LOG_NODE.severe("Failed to read image data: " + e.getLocalizedMessage());
			return;
		}
	}

	/**
	 * JavaBean-style getter-method for colorModelPort
	 * 
	 * @return the colorModelPort
	 */
	public OutputPort getColorModelPort() {
		return colorModelPort;
	}

	/**
	 * /** JavaBean-style setter-method for colorModelPort
	 * 
	 * @param colorModelPort
	 *            the colorModelPort to set
	 */
	public void setColorModelPort(OutputPort colorModelPort) {
		this.colorModelPort = colorModelPort;
	}

	/**
	 * JavaBean-style getter-method for rasterPort
	 * 
	 * @return the rasterPort
	 */
	public OutputPort getRasterPort() {
		return bufferedImagePort;
	}

	/**
	 * /** JavaBean-style setter-method for rasterPort
	 * 
	 * @param rasterPort
	 *            the rasterPort to set
	 */
	public void setRasterPort(OutputPort rasterPort) {
		this.bufferedImagePort = rasterPort;
	}

	/**
	 * JavaBean-style getter-method for bufferedImagePort
	 * 
	 * @return the bufferedImagePort
	 */
	public OutputPort getBufferedImagePort() {
		return bufferedImagePort;
	}

	/**
	 * /** JavaBean-style setter-method for bufferedImagePort
	 * 
	 * @param bufferedImagePort
	 *            the bufferedImagePort to set
	 */
	public void setBufferedImagePort(OutputPort bufferedImagePort) {
		this.bufferedImagePort = bufferedImagePort;
	}

	/**
	 * JavaBean-style getter-method for widthPort
	 * 
	 * @return the widthPort
	 */
	public OutputPort getWidthPort() {
		return widthPort;
	}

	/**
	 * /** JavaBean-style setter-method for widthPort
	 * 
	 * @param widthPort
	 *            the widthPort to set
	 */
	public void setWidthPort(OutputPort widthPort) {
		this.widthPort = widthPort;
	}

	/**
	 * JavaBean-style getter-method for heightPort
	 * 
	 * @return the heightPort
	 */
	public OutputPort getHeightPort() {
		return heightPort;
	}

	/**
	 * /** JavaBean-style setter-method for heightPort
	 * 
	 * @param heightPort
	 *            the heightPort to set
	 */
	public void setHeightPort(OutputPort heightPort) {
		this.heightPort = heightPort;
	}

	/**
	 * JavaBean-style getter-method for typePort
	 * 
	 * @return the typePort
	 */
	public OutputPort getTypePort() {
		return typePort;
	}

	/**
	 * /** JavaBean-style setter-method for typePort
	 * 
	 * @param typePort
	 *            the typePort to set
	 */
	public void setTypePort(OutputPort typePort) {
		this.typePort = typePort;
	}

	/**
	 * JavaBean-style getter-method for imageURI
	 * 
	 * @return the imageURI
	 */
	public InputPort getFilename() {
		return imageURI;
	}

	/**
	 * /** JavaBean-style setter-method for imageURI
	 * 
	 * @param imageURI
	 *            the imageURI to set
	 */
	public void setFilename(InputPort filename) {
		this.imageURI = filename;
	}
}
